<template>
  <div class="constant-type">
    <el-card>
      <template #header>
        <div class="card-header">
          <span>常数类型管理</span>
          <el-button type="primary" @click="handleCreate">新增</el-button>
        </div>
      </template>

      <el-table :data="typeList" style="width: 100%">
        <el-table-column prop="id" label="ID" width="80" />
        <el-table-column prop="constanttypecode" label="编码" width="120" />
        <el-table-column prop="constanttypename" label="名称" />
        <el-table-column label="操作" width="180">
          <template #default="scope">
            <el-button size="small" @click="handleEdit(scope.row)">编辑</el-button>
            <el-button size="small" type="danger" @click="handleDelete(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>

    <!-- 表单对话框 -->
    <el-dialog v-model="dialogVisible" title="常数类型表单" width="30%">
      <el-form :model="form" label-width="80px">
        <el-form-item label="编码">
          <el-input v-model="form.constanttypecode" placeholder="输入编码" />
        </el-form-item>
        <el-form-item label="名称">
          <el-input v-model="form.constanttypename" placeholder="输入名称" />
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="dialogVisible = false">取消</el-button>
          <el-button type="primary" @click="handleSave">保存</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'
import service from '@/utils/request'

// 数据状态
const typeList = ref([])
const dialogVisible = ref(false)
const form = ref({
  id: null,
  constanttypecode: '',
  constanttypename: ''
})
const currentAction = ref('create') // create/update

// 生命周期钩子 - 组件挂载时获取数据
onMounted(() => {
  fetchData()
})

const fetchData = async () => {
  try {
    const response = await service.get('/constanttype/all') // 修改为正确的API路径
    typeList.value = response
  } catch (error) {
    console.error('获取常数类型列表失败', error)
    ElMessage.error('获取常数类型列表失败，请检查网络连接')
  }
}

// 新增
const handleCreate = () => {
  resetForm()
  dialogVisible.value = true
  currentAction.value = 'create'
}

// 编辑
const handleEdit = (row) => {
  form.value = { ...row }
  dialogVisible.value = true
  currentAction.value = 'update'
}

// 重置表单
const resetForm = () => {
  form.value = {
    id: null,
    constanttypecode: '',
    constanttypename: ''
  }
}

// 保存
const handleSave = async () => {
  try {
    let result
    if (currentAction.value === 'create') {
      result = await service.post('/constanttype/add', form.value)
    } else {
      result = await service.post('/constanttype/update', form.value)
    }

    if (result) {
      dialogVisible.value = false
      ElMessage.success(currentAction.value === 'create' ? '新增成功' : '更新成功')
      fetchData() // 重新获取数据
    } else {
      ElMessage.error('操作失败')
    }
  } catch (error) {
    console.error('保存失败', error)
    ElMessage.error('操作失败，请检查网络连接')
  }
}

// 删除
const handleDelete = async (row) => {
  try {
    // 确认对话框
    await ElMessageBox.confirm('确定要删除这个常数类型吗？', '提示', {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      type: 'warning'
    })

    const result = await service.post('/constanttype/delete', row)
    if (result) {
      ElMessage.success('删除成功')
      fetchData() // 重新获取数据
    } else {
      ElMessage.error('删除失败')
    }
  } catch (error) {
    // 用户取消操作
    if (error.name !== 'CancelError') {
      console.error('删除失败', error)
      ElMessage.error('删除失败，请检查网络连接')
    }
  }
}
</script>

<style scoped>
.constant-type {
  padding: 20px;
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}
</style>
